﻿using RBACDemo.Ability.Docking.UserApp;
using Youshow.Ace.AspNetCore.Jwt;

namespace RBACDemo.Ability;

public class JwtDataHandler(
    IUserService userService) : AuthorizationDataPipelineHandler
{
    public override async Task<List<ModelPermission>> GetModelPermissionsAsync(PermissionsContext context)
    {
        var userId = Convert.ToInt64(context.GetClaimsUserId());
        var userDtos = await userService.GetUsersAsync(userId);
        var apiAuths = userDtos.FirstOrDefault()?.ApiAuths;
        return await Task.Run(() =>
        {
            List<ModelPermission> list = new();
            // （8.0.0）对于控制器API的路由支持对象需要使用ApiPermission对象而不是之前的ModelPermission对象。
            apiAuths.ForEach(a =>
            {
                list.Add(new ApiPermission(userId: userId.ToString(), areaName: a.AreaName,
                    controllerName: a.ControllerName, actionName: a.ActionName, method: a.Method)
                {
                    RoutePath = a.RoutePath
                });
            });
            return list;
        });
    }
}